//
//  LockEventsViewController.swift
//  WNLockDemo
//
//  Created by 裴威 on 05/09/2018.
//  Copyright © 2018 wunu. All rights reserved.
//

import UIKit
import PKHUD

class LockEventsViewController: UIViewController {
    
    @IBOutlet weak var authorizedButton: UIButton!
    @IBOutlet weak var temporaryButton: UIButton!
    @IBOutlet weak var tableView: UITableView!
    
    var lockId: Int?
    private var pageIndex: Int = 0
    private var data: [WNLockEventModel] = [] {
        didSet {
            if data.count == 0 {
                showEmptyView(message: "暂无数据")
            } else {
                hideEmptyView()
            }
            tableView.reloadData()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "开锁记录"
        configureNavigationBarBack()
        tableView.tableFooterView = UIView(frame: CGRect(x: 0,y: 0, width: 0, height: 0.1))
        fetchEvents(withGroup: 0)
    }
    
    @IBAction func handleChooseButtonEvent(_ sender: UIButton) {
        if sender.isSelected {
            return
        }
        sender.isSelected = true
        if sender == authorizedButton {
            temporaryButton.isSelected = false
            fetchEvents(withGroup: 0)
        } else {
            authorizedButton.isSelected = false
            fetchEvents(withGroup: 1)
        }
    }
    
    private func fetchEvents(withGroup group: Int) {
        if let id = lockId {
            WNLockApi.lockEvent(lockId: id, group: group, pageIndex: pageIndex, pageSize: 20).execute { (result: FxResponseResult<[WNLockEventModel]>) in
                switch result {
                case .success(let events):
                    self.data = events
                case .failure(let error):
                    if self.data.count == 0 {
                        self.showEmptyView(message: "暂无数据")
                    } else {
                        self.hideEmptyView()
                    }
                    HUD.flash(.label(error.errMsg), delay: 1.5)
                }
            }
        }
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */
    
}

extension LockEventsViewController: UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "EventCell") as! LockEventTableViewCell
        cell.loadData(data: data[indexPath.row])
        return cell
    }
}

extension LockEventsViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
}

